package cn.edu.njust.alg.hdu;

import java.util.Scanner;

public class Main {

	public static int[] parent = new int[1002];
	public static boolean[] t = new boolean[1002];

	public static void init() {
		for (int i = 0; i < parent.length; i++) {
			parent[i] = i;
		}
	}

	public static int find(int x) {
		int r = x;
		while (parent[r] != r) {
			r = parent[r];
		}
		int i = x, j;
		while (i != r) {
			j = parent[i];
			parent[i] = r;
			i = j;
		}
		return r;
	}

	public static void join(int x, int y) {
		int px = find(x);
		int py = find(y);
		if (px != py) {
			parent[px] = py;
		}
	}

	public static void main(String[] args) {

		int n, m;
		Scanner sc = new Scanner(System.in);
		int i, x, y;
		while (true) {
			n = sc.nextInt();
			if (n == 0) {
				sc.close();
				return;
			}
			m = sc.nextInt();
			init();
			for (i = 0; i < m; i++) {
				x = sc.nextInt();
				y = sc.nextInt();
				join(x, y);
			}
			for (i = 1; i <= n; i++) {
				t[i] = false;
			}
			for (i = 1; i <= n; i++) {
				t[find(i)] = true;
			}
			int count = 0;
			for (i = 1; i <= n; i++) {
				if (t[i]) {
					count++;
				}
			}

			System.out.println(count - 1);

		}
	}
}
